/**
 * @method
 * @desc 字符串=>base64
 * @version 1.1
 * @date 2018-08-28
 * @author Leo Yang
 */
const stringToBase64 = (str: string): string => btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_, p: number) => String.fromCharCode(parseInt('0x' + p))))

/**
 * @method
 * @desc base64=>字符串
 * @version 1.1
 * @date 2018-08-28
 * @author Leo Yang
 */
const base64ToString = (base: string): string => decodeURIComponent(atob(base).split('').map((c: string) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))

/**
 * @method
 * @desc 判断移动端平台
 * @version 0.3
 * @date 2018-08-28
 * @author Leo Yang
 */
const browser = (): string => {
  if (/android/i.test(navigator.userAgent)) {
    return 'android'
  } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    return 'ios'
  } else {
    return 'other'
  }
}

/**
 * @method
 * @desc 异步函数中跳转页面
 * @version 0.3
 * @date 2018-08-28
 * @author Leo Yang
 */
const asyncWindowOpen = (url: Location): void => {
  if (browser() === 'ios' && /Safari/i.test(navigator.userAgent) && !/Chrome/i.test(navigator.userAgent)) {
    window.location = url
  } else {
    window.open(url.toString())
  }
}

/**
 * @method
 * @desc 正则检查
 * @version 0.3
 * @date 2018-08-28
 * @author Leo Yang
 */
const regExp = {
  // 检查用户名
  testName (name: string): boolean {
    // return /^(?![0-9]+$)[0-9A-Za-z]{4,10}$/.test(name)
    return /^[0-9A-Za-z]{4,11}$/.test(name)
  },
  // 检查密码
  testPass (pass: string): boolean {
    return /^[A-Za-z\d]{6,12}$/.test(pass)
  },
  // 检查图形验证码
  testVcode (vcode: string): boolean {
    return /^[A-Za-z\d]{4}$/.test(vcode)
  },
  // 检查手机号码
  testPhone (phone: string): boolean {
    return /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/.test(phone)
  },
  // 检查QQ号码
  testQQ (qq: string): boolean {
    return /^[1-9]\d{4,10}$/.test(qq)
  },
  // 检查微信号码
  testWechat (wechat: string): boolean {
    return /^[a-zA-Z0-9_]{6,20}$/.test(wechat)
  },
  // 检查email账号
  testEmail (email: string): boolean {
    return /^\w[\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/.test(email)
  },
  // 是否是中文
  testChinese (chinese: string): boolean {
    return /[\u4e00-\u9fa5]/.test(chinese)
  },
  // 检查基本银行卡号
  testBank (bank: string): boolean {
    return /^(\d{16}|\d{19})$/.test(bank)
  }
}

/**
 * @method
 * @desc cookies 操作封装
 * @version 0.5
 * @date 2018-08-29
 * @author Leo Yang
 */
const cookies = {
  // 读取
  get (name: string): string | null {
    const cookieStr: string = `; ${document.cookie}; `
    const index: number = cookieStr.indexOf(`; ${name}=`)
    if (index !== -1) {
      const s: string = cookieStr.substring(index + name.length + 3, cookieStr.length)
      return unescape(s.substring(0, s.indexOf('; ')))
    } else {
      return null
    }
  },
  // 设置
  set (name: string, value: string, expires: number): void {
    const expDays: number = expires * 24 * 60 * 60 * 1000
    const expDate: Date = new Date()
    expDate.setTime(expDate.getTime() + expDays)
    const expString: string = expires ? `Expires=${expDate.toUTCString()}` : ''
    const pathString: string = 'path=/'
    document.cookie = `${name}=${escape(value)};${expString};${pathString}`
  },
  // 删除
  remove (name: string): void {
    const exp: Date = new Date(new Date().getTime() - 1)
    document.cookie = `${name}='';expires=${exp.toUTCString()};path=/`
  }
}

export {
  stringToBase64,
  base64ToString,
  browser,
  asyncWindowOpen,
  regExp,
  cookies
}
